//
//  PurchaseHistoryCell.m
//  RedballBar
//
//  Created by Faith on 2024/8/31.
//

#import "SchemePartCell.h"

@interface SchemePartCell()

@property (nonatomic, strong) UIImageView *avataView;

@property (nonatomic, strong) UILabel *personName;

@property (nonatomic, strong) UILabel *personSummary;

@property (nonatomic, strong) UILabel *schemeTitle;

@property (nonatomic, strong) UILabel *schemeSummary;

@property (nonatomic, strong) UIView *schemeMatchContainer;

@property (nonatomic, strong) UILabel *schemeMatchTitle;

@property (nonatomic, strong) UILabel *schemeDeadline;

@property (nonatomic, strong) UILabel *schemeViews;

@property (nonatomic, strong) UILabel *schemeAmount;

@property (nonatomic, strong) TTGTextTagCollectionView *tagView;


@end

@implementation SchemePartCell

- (instancetype)initWithFrame:(CGRect)frame {
    id instance = [super initWithFrame:frame];
    [self layoutItems];
    return instance;
}

- (void)layoutItems {
    UIView *contentView = [[UIView alloc] init];
    //contentView.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
    [self.contentView addSubview:contentView];
    [contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    // 头像
    _avataView = [[UIImageView alloc] init];
    _avataView.layer.cornerRadius = 17.5;
    _avataView.layer.masksToBounds = YES;
    [contentView addSubview:_avataView];
    [_avataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentView).offset(SIZE(14));
        make.left.equalTo(contentView).offset(SIZE(14));
        make.size.mas_equalTo(@35);
    }];
    
    // 昵称
    _personName = [[UILabel alloc] init];
    _personName.numberOfLines = 0;
    _personName.font = [UIFont systemFontOfSize:FONT_SIZE(14) weight:UIFontWeightBold];
    _personName.textColor = [UIColor colorWithHexString:@"#343434"];
    [contentView addSubview:_personName];
    [_personName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_avataView);
        make.left.equalTo(_avataView.mas_right).offset(SIZE(15));
    }];
    
    // 简介
    _personSummary = [[UILabel alloc] init];
    _personSummary.font = [UIFont systemFontOfSize:FONT_SIZE(10)];
    _personSummary.textColor = [UIColor colorWithHexString:@"#898989"];
    [contentView addSubview:_personSummary];
    [_personSummary mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_personName.mas_bottom).offset(5);
        make.left.equalTo(_personName);
        make.right.equalTo(contentView).offset(-SIZE(14));
    }];
    
    // 方案名称
    _schemeTitle = [[UILabel alloc] init];
    _schemeTitle.numberOfLines = 0;
    _schemeTitle.font = [UIFont systemFontOfSize:FONT_SIZE(13) weight:UIFontWeightBold];
    _schemeTitle.textColor = [UIColor colorWithHexString:@"#212121"];
    [contentView addSubview:_schemeTitle];
    [_schemeTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_personSummary.mas_bottom).offset(15);
        make.left.equalTo(_avataView);
        make.right.equalTo(contentView).offset(-(SIZE(15)));
    }];
    
    // 方案简介
    _schemeSummary = [[UILabel alloc] init];
    _schemeSummary.numberOfLines = 0;
    _schemeSummary.font = [UIFont systemFontOfSize:FONT_SIZE(11) weight:UIFontWeightRegular];
    _schemeSummary.textColor = [UIColor colorWithHexString:@"#212121"];
    [contentView addSubview:_schemeSummary];
    [_schemeSummary mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_schemeTitle.mas_bottom).offset(15);
        make.left.equalTo(_schemeTitle);
        make.right.equalTo(_schemeTitle);
    }];
    
    // 方案正在进行的比赛容器
    _schemeMatchContainer = [[UIView alloc] init];
    _schemeMatchContainer.layer.cornerRadius = 5;
    _schemeMatchContainer.layer.masksToBounds = YES;
    _schemeMatchContainer.backgroundColor = [UIColor colorWithHexString:@"#F9FAFC"];
    [contentView addSubview:_schemeMatchContainer];
    [_schemeMatchContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_schemeSummary.mas_bottom);
        make.left.equalTo(_schemeTitle);
        make.right.equalTo(_schemeTitle);
    }];
    
    // 方案正在进行的比赛
    _schemeMatchTitle = [[UILabel alloc] init];
    _schemeMatchTitle.numberOfLines = 0;
    _schemeMatchTitle.font = [UIFont systemFontOfSize:FONT_SIZE(11) weight:UIFontWeightRegular];
    _schemeMatchTitle.textColor = [UIColor colorWithHexString:@"#212121"];
    [_schemeMatchContainer addSubview:_schemeMatchTitle];
    [_schemeMatchTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_schemeMatchContainer).offset(6);
        make.left.equalTo(_schemeMatchContainer).offset(6);
        make.right.equalTo(_schemeMatchContainer).offset(-6);
        make.bottom.equalTo(_schemeMatchContainer).offset(-6);
    }];
    
    // 方案标签
    _tagView = [[TTGTextTagCollectionView alloc] init];
    [contentView addSubview:_tagView];
    [_tagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_schemeMatchContainer.mas_bottom).offset(15);
        make.left.equalTo(_schemeTitle);
        make.right.equalTo(_schemeTitle);
    }];
    
    // 方案截止时间
    _schemeDeadline = [[UILabel alloc] init];
    _schemeDeadline.numberOfLines = 0;
    _schemeDeadline.font = [UIFont systemFontOfSize:FONT_SIZE(10)];
    _schemeDeadline.textColor = [UIColor colorWithHexString:@"#F03B37"];
    [contentView addSubview:_schemeDeadline];
    [_schemeDeadline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_tagView.mas_bottom).offset(15);
        make.left.equalTo(_schemeTitle);
    }];
    
    // 方案浏览人数
    _schemeViews = [[UILabel alloc] init];
    _schemeViews.numberOfLines = 0;
    _schemeViews.font = [UIFont systemFontOfSize:FONT_SIZE(10)];
    _schemeViews.textColor = [UIColor colorWithHexString:@"#898989"];
    [contentView addSubview:_schemeViews];
    [_schemeViews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_schemeDeadline);
        make.left.equalTo(_schemeDeadline.mas_right).offset(SIZE(19));
    }];
    
    // 方案金额
    _schemeAmount = [[UILabel alloc] init];
    _schemeAmount.numberOfLines = 0;
    _schemeAmount.font = [UIFont systemFontOfSize:FONT_SIZE(10)];
    _schemeAmount.textColor = [UIColor colorWithHexString:@"#F03B37"];
    [contentView addSubview:_schemeAmount];
    [_schemeAmount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_schemeDeadline);
        make.left.greaterThanOrEqualTo(_schemeViews.mas_right).offset(SIZE(10));
        make.right.equalTo(_schemeTitle);
    }];
    
    // 分割线
    UIView *sepratorLine = [[UIView alloc] init];
    sepratorLine.backgroundColor = [UIColor colorWithHexString:@"#E8E8E8"];
    [contentView addSubview:sepratorLine];
    [sepratorLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_schemeDeadline.mas_bottom).offset(15);
        make.left.equalTo(_avataView);
        make.right.equalTo(contentView).offset(-SIZE(14));
        make.height.mas_equalTo(1);
        make.bottom.equalTo(contentView);
    }];
}

/**
 * 设置数据
 */
- (void)setData:(NSDictionary *)data {
    [_avataView sd_setImageWithURL:[NSURL URLWithString:data[@"personAvatar"]]];
    _personName.text = [NSString stringWithFormat:@"%@", data[@"personName"]];
    _personSummary.text = [NSString stringWithFormat:@"%@", data[@"personSummary"]];
    _schemeTitle.text = [NSString stringWithFormat:@"%@", data[@"schemeTitle"]];
    _schemeSummary.text = [NSString stringWithFormat:@"%@", data[@"schemeSummary"]];
    NSString *c = [NSString stringWithFormat:@"%@", data[@"schemeMatchTitle"]];
    if(c.length!=0) {
        _schemeMatchTitle.text = c;
        [_schemeMatchContainer mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_schemeSummary.mas_bottom).offset(15);
        }];
        
        [_schemeMatchTitle mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_schemeMatchContainer).offset(6);
            make.left.equalTo(_schemeMatchContainer).offset(6);
            make.right.equalTo(_schemeMatchContainer).offset(-6);
            make.bottom.equalTo(_schemeMatchContainer).offset(-6);
        }];
    } else {
        _schemeMatchTitle.text = @"";
        [_schemeMatchContainer mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_schemeSummary.mas_bottom).offset(0);
        }];
        
        [_schemeMatchTitle mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_schemeMatchContainer).offset(0);
            make.left.equalTo(_schemeMatchContainer).offset(0);
            make.right.equalTo(_schemeMatchContainer).offset(0);
            make.bottom.equalTo(_schemeMatchContainer).offset(0);
        }];
    }
    
    [_tagView removeAllTags];
    TTGTextTag *tag1 = [TTGTextTag tagWithContent:[TTGTextTagStringContent contentWithText:@"实现标签的自适应"] style:[TTGTextTagStyle new]];
    TTGTextTag *tag2 = [TTGTextTag tagWithContent:[TTGTextTagStringContent contentWithText:@"iOS 项目"] style:[TTGTextTagStyle new]];
    TTGTextTag *tag3 = [TTGTextTag tagWithContent:[TTGTextTagStringContent contentWithText:@"复杂的布局管理"] style:[TTGTextTagStyle new]];
    TTGTextTag *tag4 = [TTGTextTag tagWithContent:[TTGTextTagStringContent contentWithText:@"编辑器打开"] style:[TTGTextTagStyle new]];
    TTGTextTag *tag5 = [TTGTextTag tagWithContent:[TTGTextTagStringContent contentWithText:@"动态标签"] style:[TTGTextTagStyle new]];
    
    tag1.style.extraSpace = CGSizeMake(10, 6);
    tag2.style.extraSpace = CGSizeMake(10, 6);
    tag3.style.extraSpace = CGSizeMake(10, 6);
    tag4.style.extraSpace = CGSizeMake(10, 6);
    tag5.style.extraSpace = CGSizeMake(10, 6);
    ((TTGTextTagStringContent*)tag1.content).textFont = [UIFont systemFontOfSize:FONT_SIZE(9)];
    ((TTGTextTagStringContent*)tag2.content).textFont = [UIFont systemFontOfSize:FONT_SIZE(9)];
    ((TTGTextTagStringContent*)tag3.content).textFont = [UIFont systemFontOfSize:FONT_SIZE(9)];
    ((TTGTextTagStringContent*)tag4.content).textFont = [UIFont systemFontOfSize:FONT_SIZE(9)];
    ((TTGTextTagStringContent*)tag5.content).textFont = [UIFont systemFontOfSize:FONT_SIZE(9)];
    [_tagView addTags:@[tag1,tag2,tag3,tag4,tag5]];
    [_tagView reload];
    
    _schemeDeadline.text = [NSString stringWithFormat:@"%@ 后截止", data[@"schemeDeadline"]];
    _schemeViews.text = [NSString stringWithFormat:@"%@ 人查看", data[@"schemeViews"]];
    _schemeAmount.text = [NSString stringWithFormat:@"%@", data[@"schemeAmount"]];
    
    [self layoutIfNeeded];
}

@end
